package com.geek.controller;



import com.geek.bean.BaseRespVo;
import com.geek.bean.vo.FeedbackSubmitVo;
import com.geek.model.MarketFeedback;
import com.geek.model.MarketUser;
import com.geek.service.FeedbackService;
import com.geek.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/wx/feedback")
public class WxFeedbackController {
    @Autowired
    UserService userService;
    @Autowired
    FeedbackService feedbackService;
    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody FeedbackSubmitVo feedbackSubmitVo) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            Integer userId = (Integer) subject.getPrincipal();
            MarketUser marketUser = userService.findById(userId);
            Date date = new Date();
            MarketFeedback marketFeedback = MarketFeedback.builder()
                    .userId(marketUser.getId())
                    .username(marketUser.getUsername())
                    .mobile(feedbackSubmitVo.getMobile())
                    .feedType(feedbackSubmitVo.getFeedType())
                    .content(feedbackSubmitVo.getContent())
                    .hasPicture(feedbackSubmitVo.isHasPicture())
                    .picUrls(feedbackSubmitVo.getPicUrls())
                    .addTime(date)
                    .updateTime(date)
                    .deleted(false)
                    .build();
            feedbackService.insert(marketFeedback);
            return BaseRespVo.ok(null);
        }else {
            return BaseRespVo.unAuthc();
        }
    }
}
